import http from './http.js';

class CardConsumption {
  constructor(data) {
    this.id = data.id;
    this.order_id = data.order_id;
    this.income_type = data.income_type;
    this.transaction_type = data.transaction_type;
    this.store_id = data.store_id;
    this.card_package_id = data.card_package_id;
    this.user_id = data.user_id;
    this.staff_id = data.staff_id;
    this.product_id = data.product_id;
    this.consumption_date = data.consumption_date;
    this.amount_used = data.amount_used;
    this.remaining_amount = data.remaining_amount;
    this.card_type = data.card_type;
    this.description = data.description;
  }

  static fromApi(data) {
    return new CardConsumption(data);
  }
}

export default {
  /**
   * 获取消费记录列表
   * @param {Object} params 查询参数
   * @returns {Promise<{data: CardConsumption[]}>}
   */
  async getList(params = {}) {
    try {
      const response = await http.get('/card_consumptions', { params });
      return response.data;
    } catch (error) {
      console.error('获取消费记录列表失败:', error);
      throw error;
    }
  },

  /**
   * 获取用户消费记录
   * @param {Object} params 包含user_id的查询参数
   * @returns {Promise<{data: CardConsumption[]}>}
   */
  async getUserConsumptions(params = {}) {
    try {
      const response = await http.get('/card_consumptions/user', params );
      return response.data;
    } catch (error) {
      console.error('获取用户消费记录失败:', error);
      throw error;
    }
  },

  /**
   * 获取员工提成记录
   * @param {Object} params 包含staff_id的查询参数
   * @returns {Promise<{data: CardConsumption[]}>}
   */
  async getStaffCommissions(params = {}) {
    try {
      const response = await http.get('/card_consumptions/staff', params );
      return response.data;
    } catch (error) {
      console.error('获取员工提成记录失败:', error);
      throw error;
    }
  },
  /**
   * 获取店铺统计信息
   * @param {Object} params 包含store_id的查询参数(可选)
   * @returns {Promise<{
  *   store_count: number,
  *   staff_count: number,
  *   customer_count: number,
  *   order_count: number,
  *   total_order_amount: number,
  *   recharge_count: number,
  *   total_recharge_amount: number,
  *   total_cost: number,
  *   total_commission_cost: number,
  *   net_income: number
  * }>}
  */
 async getStoreStatistics(params = {}) {
   try {
     const response = await http.get('/card_consumptions/store/cal', params);
     return response.data;
   } catch (error) {
     console.error('获取店铺统计信息失败:', error);
     throw error;
   }
 },
   /**
   * 获取员工业绩数据
   * @param {Object} params 查询参数
   * @param {string} params.store_id 店铺ID
   * @param {string} params.type 时间类型(day/month/year)
   * @returns {Promise<{data: Array<{staff_name: string, performance: number, commission: number}>}>}
   */
   async getStaffPerformance(params = {}) {
    try {
      const response = await http.get('/card_consumptions/staff/performance', params);
      return response.data;
    } catch (error) {
      console.error('获取员工业绩数据失败:', error);
      throw error;
    }
  },
    /**
   * 新增收支记录
   * @param {Object} data 包含以下字段:
   *   - store_id: 店铺ID(必填)
   *   - staff_id: 员工ID(必填)
   *   - transaction_type: 交易类型(1:卡包充值, 2:成本支出, 3:现金单次消费, 4:卡包消费, 5:卡包提成, 6:单笔业务提成)(必填)
   *   - income_type: 收支类型(0:支出, 1:收入)(必填)
   *   - amount_used: 金额(必填)
   *   - description: 备注(可选)
   *   - card_package_id: 卡包ID(可选)
   *   - user_id: 用户ID(可选)
   *   - product_id: 产品ID(可选)
   * @returns {Promise<{status: string, message: string, data: Object}>}
   */
    async addConsumption(data) {
      try {
        const response = await http.post('/card_consumptions/transaction', data);
        return response.data;
      } catch (error) {
        console.error('新增收支记录失败:', error);
        throw error;
      }
    },
    /**
     * 获取用户收支明细数据
     * @param {Object} params 查询参数
     * @param {string} params.user_id 用户ID(必填)
     * @param {string} params.start_date 开始日期(可选)
     * @param {string} params.end_date 结束日期(可选)
     * @param {number} params.page 页码(可选)
     * @param {number} params.limit 每页数量(可选)
     * @returns {Promise<{
     *   status: string,
     *   message: string,
     *   data: {
     *     list: Array<{
     *       id: number,
     *       transaction_type: number,
     *       income_type: number,
     *       amount_used: number,
     *       consumption_date: string,
     *       consumption_day: number,
     *       consumption_month: number,
     *       consumption_year: number,
     *       user_name: string,
     *       product_name: string,
     *       description: string
     *     }>,
     *     pagination: {
     *       total: number,
     *       page: number,
     *       limit: number
     *     }
     *   }
     * }>}
     */
    async getUserTransactions(params = {}) {
      try {
        console.log("===========",params);
        const response = await http.get('/card_consumptions/user/transactions', params);
        console.log("===========",response);
        return response.data;
      } catch (error) {
        console.error('获取用户收支明细数据失败:', error);
        throw error;
      }
    },
};